package realestate.manager.request;

import realestate.entity.ObjectOnMap;
import realestate.entity.ObjectOnMapShort;
import realestate.entity.Owner;

import java.util.Set;
import java.util.TreeSet;

/**
 * Created by IntelliJ IDEA.
 * User: Admin
 * Date: 12.04.11
 * Time: 18:17
 * To change this template use File | Settings | File Templates.
 */
public abstract class ObjectOnMapShortRequest implements IRequest {

	public static final String TABLE_ALIAS = "ra";
	public static final String CLASS_REPLCATE_MARK = "!1";
	public static final String OWNER_ID_STRING_PREFIX = ObjectOnMapShort.FIELD_OWNER+"."+Owner.FIELD_ID;

	private StringBuilder filter_string_builder = new StringBuilder(3000);

	protected static String select_field_set;

	static {
		StringBuilder select_field_set_builder = new StringBuilder(2000);
		select_field_set_builder.append("new ").append(CLASS_REPLCATE_MARK).append("(");
		select_field_set_builder.append(buildAliasedString(ObjectOnMapShort.FIELD_ID)).append(",");
		select_field_set_builder.append(buildAliasedString(ObjectOnMapShort.FIELD_LATTITUDE)).append(",");
		select_field_set_builder.append(buildAliasedString(ObjectOnMapShort.FIELD_LONGTITUDE)).append(")");
		select_field_set = select_field_set_builder.toString();
	}

	private static final Set<String> columns = new TreeSet<String>();

	static {
		columns.add(ObjectOnMap.FIELD_LATTITUDE);
		columns.add(ObjectOnMap.FIELD_LONGTITUDE);
		columns.add(ObjectOnMap.FIELD_OWNER);
	}

	private static String buildAliasedString(String value) {
		return TABLE_ALIAS+"."+value;
	}


	protected abstract String getClassName();

	@Override
	public void addParameter(String name, Object value, LimitModifier modifier) {

		if(name.equals(ObjectOnMap.FIELD_OWNER)) {
			appendValueToBuilder(OWNER_ID_STRING_PREFIX, value, modifier);
		} else if(isRequestParameter(name)) {
			appendValueToBuilder(name, value, modifier);
		}
	}

	protected StringBuilder getFilterBuilder() {
		return filter_string_builder;
	}

	protected void appendValueToBuilder(String name, Object value, LimitModifier modifier) {
		StringBuilder container = getFilterBuilder();
		container.append(" and ").append(buildAliasedString(name)).append(" ");
		container.append(modifier.getOperationString()).append(" ");
		container.append(value.toString());
	}

	protected boolean isRequestParameter(String parameter_name) {
		return columns.contains(parameter_name);
	}

	public String buildQueryString() {
		String filter_string = buildFilterString().trim();
		if(filter_string.length() > 0) {
			filter_string = filter_string.replaceFirst("and", " where ");
		}

		filter_string_builder.delete(0, filter_string.length());
		filter_string_builder.append("select ");
		filter_string_builder.append(select_field_set);
		filter_string_builder.append(" from ");
		filter_string_builder.append(getClassName());
		filter_string_builder.append(" ");
		filter_string_builder.append(TABLE_ALIAS);
		filter_string_builder.append(filter_string);

		return filter_string_builder.toString();
	}


	public String buildFilterString() {
		return filter_string_builder.toString();
	}
}
